<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center @click="imgUploadFn">
        <template #default>
          <van-image round class="avatar" :src="$store.state.headImgUrl" />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>

      <!-- 姓名 -->
      <van-cell
        title="名称"
        is-link
        :value="editUserObj.mobile"
        @click="gaiNameFn"
      />
      <van-dialog
        v-model="show"
        title="修改名称"
        show-cancel-button
        :before-close="beforecloseFn"
      >
        <input type="text" v-fofo v-model="userName" />
      </van-dialog>

      <!-- 生日 -->
      <van-cell
        title="生日"
        is-link
        :value="editUserObj.birthday"
   @click="showBirthFn"
      />
      <van-popup
        v-model="isShowBirth"
        position="bottom"
        style="height: 50%"
        round
      >
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          title="选择出生日期"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="gaiBirthFn"
        />
      </van-popup>
      <van-cell title="性别" is-link :value="sexObj[editUserObj.gender]" />
    </van-cell-group>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import * as relativeTime from 'dayjs/plugin/relativeTime'
import 'dayjs/locale/zh-cn'
import { getUserMessaggeAPI, updateProfileAPI, editUserMessageAPI } from '@/api'

import { Notify } from 'vant'
import { mapMutations } from 'vuex'
// 插件
dayjs.locale('zh-cn') // use locale
dayjs.extend(relativeTime)
export default {
  data () {
    return {
      editUserObj: [], // 编辑用户资料
      sexObj: {
        // 铺设用户的性别和生日时尤其注意,他是value值
        1: '男',
        2: '女',
        '-1': '保密'
      },
      userName: '',
      show: false,
      minDate: new Date(1950, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(2021, 9, 21),
      isShowBirth: false
    }
  },
  name: 'UserEdit',

  async created () {
    const res = await getUserMessaggeAPI()
    // console.log(res)
    this.editUserObj = res.data.data
  },
  methods: {
    ...mapMutations(['setheadImgUrlMutations']),
    // 图片点击事件
    imgUploadFn () {
      this.$refs.iptFile.click()
    },
    // 用户头像修改
    async onFileChange (ev) {
      console.log(ev.target.files[0])
      const formData = new FormData()
      // 加入参数值和名
      formData.append('photo', ev.target.files[0]) // photo在表单里参数名携带
      const res = await editUserMessageAPI(formData)
      console.log(res)
      this.editUserObj.photo = res.data.data.photo // 更新用户头像成功
      this.setheadImgUrlMutations(this.editUserObj.photo)
    },
    // 修改用户姓名
    gaiNameFn () {
      this.show = true
      this.userName = this.editUserObj.mobile // 默认显示的用户名
    },
    async beforecloseFn (action, done) {
      // console.log(action)confirm
      // console.log(done)cancel
      if (action === 'cancel') {
        // 用户点了取消
        done() // 关闭弹窗
      } else {
        // 点了确定
        // 判断(只能输入中英文和大小写及7位数)
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          const res = await updateProfileAPI({
            // 告诉后台修改名称
            userName: this.userName
          })
          console.log(res)
          this.editUserObj.mobile = this.userName
          done() // 关闭弹窗
        } else {
          // 警告通知
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          // 不通过的话阻止弹窗关闭
          done(false)
        }
      }
    },
    // 修改生日
    showBirthFn () {
      this.currentDate = new Date(this.editUserObj.birthday) // 设置data组件默认显示时间
      this.isShowBirth = true
    },

    async gaiBirthFn () {
      const res1 = await updateProfileAPI({
        // 告诉后台修改名称
        birthday: dayjs(this.currentDate).format('YYYY-MM-DD')
      })
      console.log(res1)
      this.isShowBirth = false// 关闭弹窗
      this.editUserObj.birthday = dayjs(this.currentDate).format('YYYY-MM-DD')
    }
  }

}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/.van-dialog {
  text-align: center;
}
/deep/.van-dialog input {
  text-align: center;
  border: none;
}
</style>
